package com.yangtzeu.jwc.business;

import java.io.IOException;

import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class NewsParser extends BaseParser {

    public String catalog;
    public String time;
    public String title;

    public Element content;
    public String info;
    public Element footer;

    public String nextPage;
    public String frontPage;
    public String css = "<style type=\"text/css\" >" +
            "*{max-width:100% !important;box-sizing:border-box !important;word-wrap:break-word !important;word-break:break-all !important;}" +
            "h1{color:#333 !important}" +
            "</style>";
    public String tableCss = "<style type=\"text/css\" >" +
            "table{max-width:100% !important;font-size:10px !important;box-sizing:border-box !important;word-wrap:break-word !important;word-break:break-all !important;}" +
            "</style>";
    public String divider = "<hr style=\"height:1px;border:none;border-top:1px solid #00aaee;\" />";

    public String createTag(String html, String tag, String attr) {
        return "<" + tag + " " + attr + ">" + html + "</" + tag + ">";
    }

    public NewsParser() {
        super();
    }

    public NewsParser(String path, String cataloge, String title, String time) {
        super(path);
        this.catalog = cataloge;
        this.title = title;
        this.time = time;
    }

    @Override
    public BaseParser startParse(ParserListener listener, boolean fromCache) {
        try {
            doc = parse(path, fromCache);
            content = doc.select("#arc_word").get(0);
            clearContent();
            footer = doc.select("#footer").get(0);
            info = doc.select("h3").get(0).text();
            title = doc.select("h1").get(0).text();
            Element front = doc.select("#arc_next li").get(0);
            front = front == null ? null : (front.select("a").size() > 0 ? front.select("a").get(0) : null);
            frontPage = front == null ? null : front.attr("href");
            Element next = doc.select("#arc_next li").get(1);
            next = next == null ? null : (next.select("a").size() > 0 ? next.select("a").get(0) : null);
            nextPage = next == null ? null : next.attr("href");
            listener.finish();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            listener.failed();
        }
        return this;
    }

    public String toWap() {
        String body = "";
        body += css;
        if (title != null) {
            body += createTag(createTag(createTag(title, "h3", null), "center", null), "div", null);
            body += divider;
        }
        if (info != null) {
            body += createTag(createTag(info, "p", null), "center", "style=\"font-size:12px !important;\"");
            body += divider;
        }
        if (content != null) {
            body += content.toString().replaceAll("</*span>", "");
        }
        if (footer != null) {
            body += divider;
            body += createTag(createTag(footer.toString(), "center", null), "div", "style=\"font-size:12px !important\"");
        }
        body = body.replace("target=\"downframe\"", "");
        return body;
    }

    public void clearContent() {
        clearBg(content);
        clearP(content);
        clearTable(content);
        clearSpan(content);
    }

    public void clearTable(Element e) {
        // 去掉table的所有属性
        Elements tables = e.select("table");
        Element table = null;
        Elements trs = null;
        Element tr = null;
        Elements tds = null;
        Element td = null;
        for (int i = 0; i < tables.size(); i++) {
            table = tables.get(i);
            removeAttr(table);
            table.attr("style", "border-collapse:collapse;border-spacing:0;border-left:1px solid #888;border-top:1px solid #888;background:#efefef;");
            trs = table.select("tr");
            for (int j = 0; j < trs.size(); j++) {
                tr = trs.get(j);
                removeAttr(tr);
                tds = tr.select("td");
                td = null;
                for (int k = 1; k < tds.size(); k++) {
                    td = tds.get(k);
                    clearTd(td);
                }
            }
        }
        Elements tds2 = e.select("td");
        for (int i = 0; i < tds2.size(); i++) {
            clearTd(tds2.get(i));
            tds2.get(i).attr("style", "border-right:1px solid #888;border-bottom:1px solid #888;");
        }
    }

    public void clearBg(Element element) {
        // 去掉所有background属性
        Elements bkcs = element.getElementsByAttribute("background-color");
        for (int i = 0; i < bkcs.size(); i++) {
            bkcs.get(i).removeAttr("background-color");
        }
    }

    public void clearP(Element e) {
        Elements ps = e.select("p");
        Element p = null;
        // 去掉p的所有属性
        for (int i = 0; i < ps.size(); i++) {
            p = ps.get(i);
            removeAttr(p);
        }
    }

    public void clearSpan(Element e) {
        Elements sps2 = e.select("span");
        for (int i = 0; i < sps2.size(); i++) {
            removeAttr(sps2.get(i));
        }
    }

    public void clearTd(Element td) {
        // 记住该格子占用几行
        String rowspan = td.attr("rowSpan");
        String colspan = td.attr("colspan");
        removeAttr(td);
        if (!"".equals(rowspan) && rowspan != null)
            td.attr("rowspan", rowspan);
        if (!"".equals(colspan) && colspan != null)
            td.attr("colspan", colspan);
        td.attr("style", "border:1px solid black");
        Elements spans = td.select("span");
        Element span = null;
        for (int l = 0; l < spans.size(); l++) {
            span = spans.get(l);
            removeAttr(span);
        }
        Elements tdds = td.select("td");
    }

    public void removeAttr(Element e) {
        if (e != null) {
            Attributes attrs = e.attributes();
            if (attrs != null) {
                for (Attribute attr : attrs.asList()) {
                    e.removeAttr(attr.getKey());
                }
            }
        }
    }

    @Override
    public String toString() {
        return "News [time=" + time + ", path=" + path + ", title=" + title + ", content=" + content + ", info=" + info + ", footer=" + footer + ", nextPage=" + nextPage + ", frontPage=" + frontPage + "]";
    }

}
